use "ESS8e02_2.dta", replace 
set scheme white_ptol
* ----------------------------------------------------------------------------
**# Data preparation
* ----------------------------------------------------------------------------
***************************************
* day variable and treatment variable *
***************************************
fre inwdds 
fre inwmms 
fre inwyys
gen date=mdy(inwmms,inwdds,inwyys)
format date %tdCCYY-NN-DD

gen tr=.
replace tr=0 if date<=td(8nov2016) 
replace tr=1 if date>td(9nov2016) 
replace tr=. if date==.
fre tr

fre cntry
tab cntry tr, m
keep if cntry=="AT" | cntry=="BE" | cntry=="CZ"  | cntry=="DE" | cntry=="EE" | cntry=="FI" | cntry=="GB" | cntry=="NL" | cntry=="SE"| cntry=="SI"  // keep EU countries: The sample of Portugal is discarded since there are only 76 respondents in the control group. Switzerland, Norway, Israel are not EU countries
tab cntry tr 
encode cntry, generate(country)
tab date tr 
fre country

* day variables
list date in 1/20
gen day = date - td(9nov2016) 
fre day

* binning the sample bi-weekly
gen biweek=.  
replace biweek=1 if day <=-57
replace biweek=2 if day>=-56 & day <=-43
replace biweek=3 if day>=-42 & day <=-29
replace biweek=4 if day>=-28 & day <=-15
replace biweek=5 if day>=-14 & day <=-1
replace biweek=6 if day>=1 & day <=14
replace biweek=7 if day>=15 & day <=28
replace biweek=8 if day>=29 & day <=42
replace biweek=9 if day>=43 & day <=56
replace biweek=10 if day>=57 & day <=70
replace biweek=11 if day>=71 & day <=84
replace biweek=12 if day>=85 
tab day biweek

***********
* outcome *
***********
* satisfaction with democracy (11-pt scale)
fre stfdem 
rename stfdem swd

* binary
gen swd_b=. 
replace swd_b=1 if swd>5
replace swd_b=0 if swd<5
replace swd_b=. if swd==.a | swd==.b
tab swd swd_b, m

**************
* covariates *
**************
rename agea age
label var age "Age"

rename gndr gender 

rename eisced edu
fre edu
replace edu=. if edu==55 // replace "other" to missing 
fre edu
label var edu "Level of education"

fre domicil //type of community

fre chldhhe //have children living in household
label define lbchldhhe  1"Children living in household: Yes" 2"Children living in household: No" 
label values chldhhe lbchldhhe 

rename blgetmg minority // Belong to minority ethnic group in country
label define lbminority 1"Minority ethnic group: Yes" 2"Minority ethnic group: No " 
label values minority lbminority

fre uemp3m // Ever unemployed and seeking work for a period more than three months
label define lbuemp3m 1"Unemployed and seeking work > 3 months: Yes" 2"Unemployed and seeking work > 3 months: No" 
label values uemp3m lbuemp3m
fre hincfel // Feeling about household's income nowadays

fre vote //  Voted last national election
label define lbvote 1"Voted last national election: Yes" 2"Voted last national election: No" 3"Not eligible to vote"
label values vote lbvote

********************
* left/right scale *
********************
fre lrscale 
clonevar lr=lrscale
fre lr
label define lblr 0"0 (Left)" 1"1" 2"2" 3"3" 4"4" 5"5" 6"6" 7"7" 8"8" 9"9" 10"10 (Right)" 
label values lr lblr
fre lr

************************
* previous vote choice *
************************
fre prtvtbat // Austria
fre prtvtcbe // Belgium
fre prtvtdcz // Czech Republic
fre prtvede2 // Germany (2nd vote: party list vote)
fre prtvtfee // Estonia
fre prtvtdfi // Finland
fre prtvtbgb // UK
fre prtvtfnl // Netherlands 
fre prtvtbse // Sweden 
fre prtvtesi // Slovenia

* radical right
gen rad_right=.
replace rad_right=1 if (prtvtbat==3 | prtvtbat==4) & cntry=="AT"
replace rad_right=0 if (prtvtbat!=3 & prtvtbat!=4) & cntry=="AT" 
replace rad_right=1 if (prtvtcbe==7 | prtvtcbe==11) & cntry=="BE"
replace rad_right=0 if (prtvtcbe!=7 & prtvtcbe!=11) & cntry=="BE"
replace rad_right=1 if prtvtdcz==7 & cntry=="CZ"
replace rad_right=0 if prtvtdcz!=7 & cntry=="CZ"
replace rad_right=1 if (prtvede2==6 | prtvede2==8) & cntry=="DE"
replace rad_right=0 if (prtvede2!=6 & prtvede2!=8) & cntry=="DE"
replace rad_right=1 if (prtvtfee==9 | prtvtfee==12)  & cntry=="EE"
replace rad_right=0 if (prtvtfee!=9 & prtvtfee!=12)  & cntry=="EE"
replace rad_right=1 if (prtvtdfi==4 | prtvtdfi==6) & cntry=="FI"
replace rad_right=0 if (prtvtdfi!=4 | prtvtdfi!=6) & cntry=="FI"
replace rad_right=1 if prtvtbgb==7 & cntry=="GB"
replace rad_right=0 if prtvtbgb!=7 & cntry=="GB"
replace rad_right=1 if prtvtfnl==3 & cntry=="NL"
replace rad_right=0 if prtvtfnl!=3 & cntry=="NL"
replace rad_right=1 if prtvtbse==10 & cntry=="SE"
replace rad_right=0 if prtvtbse!=10 & cntry=="SE"
replace rad_right=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 


* liberal
gen liberal=.
replace liberal=1 if prtvtbat==7 & cntry=="AT"
replace liberal=0 if prtvtbat!=7 & cntry=="AT"
replace liberal=1 if (prtvtcbe==8 | prtvtcbe==12) & cntry=="BE"
replace liberal=0 if (prtvtcbe!=8 & prtvtcbe!=12) & cntry=="BE"
replace liberal=1 if prtvede2==5 & cntry=="DE"
replace liberal=0 if prtvede2!=5 & cntry=="DE"
replace liberal=1 if (prtvtfee==1 | prtvtfee==2)  & cntry=="EE"
replace liberal=0 if (prtvtfee!=1 & prtvtfee!=2)  & cntry=="EE"
replace liberal=1 if prtvtdfi==1  & cntry=="FI"
replace liberal=0 if prtvtdfi!=1  & cntry=="FI"
replace liberal=1 if (prtvtbgb==3 | prtvtbgb==13) & cntry=="GB"
replace liberal=0 if (prtvtbgb!=3 & prtvtbgb!=13) & cntry=="GB"
replace liberal=1 if (prtvtfnl==1 | prtvtfnl==6) & cntry=="NL"
replace liberal=0 if (prtvtfnl!=1 & prtvtfnl!=6) & cntry=="NL"
replace liberal=1 if prtvtbse==2 & cntry=="SE"
replace liberal=0 if prtvtbse!=2 & cntry=="SE"
replace liberal=1 if prtvtesi==8 & cntry=="SI"
replace liberal=0 if prtvtesi!=8 & cntry=="SI"
replace liberal=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 

* conservative & christian democrats
gen con_christdem=.
replace con_christdem=1 if prtvtbat==2 & cntry=="AT"
replace con_christdem=0 if prtvtbat!=2 & cntry=="AT"
replace con_christdem=1 if (prtvtcbe==2 | prtvtcbe==9 | prtvtcbe==15) & cntry=="BE"
replace con_christdem=0 if (prtvtcbe!=2 & prtvtcbe!=9 & prtvtcbe!=15) & cntry=="BE"
replace con_christdem=1 if (prtvtdcz==3 | prtvtdcz==5 | prtvtdcz==6) & cntry=="CZ"
replace con_christdem=0 if (prtvtdcz!=3 & prtvtdcz!=5 & prtvtdcz!=6) & cntry=="CZ"
replace con_christdem=1 if prtvede2==1 & cntry=="DE"
replace con_christdem=0 if prtvede2!=1 & cntry=="DE"
replace con_christdem=1 if (prtvtfee==3 | prtvtfee==6 | prtvtfee==11) & cntry=="EE"
replace con_christdem=0 if (prtvtfee!=3 & prtvtfee!=6 & prtvtfee!=11) & cntry=="EE"
replace con_christdem=1 if (prtvtbgb==1 | prtvtbgb==9 | prtvtbgb==10)  & cntry=="GB"
replace con_christdem=0 if (prtvtbgb!=1 & prtvtbgb!=9 & prtvtbgb!=10)  & cntry=="GB"
replace con_christdem=1 if (prtvtfnl==5 | prtvtfnl==7) & cntry=="NL"
replace con_christdem=0 if (prtvtfnl!=5 & prtvtfnl!=7) & cntry=="NL"
replace con_christdem=1 if (prtvtbse==3 | prtvtbse==5) & cntry=="SE"
replace con_christdem=0 if (prtvtbse!=3 & prtvtbse!=5) & cntry=="SE"
replace con_christdem=1 if (prtvtesi==3 | prtvtesi==6 | prtvtesi==7) & cntry=="SI"
replace con_christdem=0 if (prtvtesi!=3 & prtvtesi!=6 & prtvtesi!=7) & cntry=="SI"
replace con_christdem=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 

* socialist & radilcal left
gen socialist_rl=.
replace socialist_rl=1 if (prtvtbat==1 | prtvtbat==6) & cntry=="AT"
replace socialist_rl=0 if (prtvtbat!=1 & prtvtbat!=6) & cntry=="AT"
replace socialist_rl=1 if (prtvtcbe==5 | prtvtcbe==6 | prtvtcbe==13 | prtvtcbe==14) & cntry=="BE"
replace socialist_rl=0 if (prtvtcbe!=5 & prtvtcbe!=6 & prtvtcbe!=13 & prtvtcbe!=14) & cntry=="BE"
replace socialist_rl=1 if (prtvtdcz==1 | prtvtdcz==2) & cntry=="CZ"
replace socialist_rl=0 if (prtvtdcz!=1 & prtvtdcz!=2) & cntry=="CZ"
replace socialist_rl=1 if (prtvede2==2 | prtvede2==3) & cntry=="DE"
replace socialist_rl=0 if (prtvede2!=2 & prtvede2!=3) & cntry=="DE"
replace socialist_rl=1 if prtvtfee==4 & cntry=="EE"
replace socialist_rl=0 if prtvtfee!=4 & cntry=="EE"
replace socialist_rl=1 if (prtvtdfi==11 | prtvtdfi==12 | prtvtdfi==15)  & cntry=="FI"
replace socialist_rl=0 if (prtvtdfi!=11 & prtvtdfi!=12 & prtvtdfi!=15)  & cntry=="FI"
replace socialist_rl=1 if (prtvtbgb==2 | prtvtbgb==11 | prtvtbgb==12) & cntry=="GB"
replace socialist_rl=0 if (prtvtbgb!=2 & prtvtbgb!=11 & prtvtbgb!=12) & cntry=="GB"
replace socialist_rl=1 if (prtvtfnl==2 | prtvtfnl==4) & cntry=="NL"
replace socialist_rl=0 if (prtvtfnl!=2 & prtvtfnl!=4) & cntry=="NL"
replace socialist_rl=1 if (prtvtbse==6 | prtvtbse==7) & cntry=="SE"
replace socialist_rl=0 if (prtvtbse!=6 & prtvtbse!=7) & cntry=="SE"
replace socialist_rl=1 if (prtvtesi==4 | prtvtesi==5 | prtvtesi==11) & cntry=="SI"
replace socialist_rl=0 if (prtvtesi!=4 & prtvtesi!=5 & prtvtesi!=11) & cntry=="SI"
replace socialist_rl=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 

* green 
gen green=.
replace green=1 if prtvtbat==5 & cntry=="AT"
replace green=0 if prtvtbat!=5 & cntry=="AT"
replace green=1 if (prtvtcbe==1 | prtvtcbe==10) & cntry=="BE"
replace green=0 if (prtvtcbe!=1 & prtvtcbe!=10) & cntry=="BE"
replace green=1 if prtvede2==4 & cntry=="DE"
replace green=0 if prtvede2!=4 & cntry=="DE"
replace green=1 if prtvtfee==5 & cntry=="EE"
replace green=0 if prtvtfee!=5 & cntry=="EE"
replace green=1 if prtvtdfi==10  & cntry=="FI"
replace green=0 if prtvtdfi!=10  & cntry=="FI"
replace green=1 if prtvtbgb==6 & cntry=="GB"
replace green=0 if prtvtbgb!=6 & cntry=="GB"
replace green=1 if (prtvtfnl==8 | prtvtfnl==10) & cntry=="NL"
replace green=0 if (prtvtfnl!=8 & prtvtfnl!=10) & cntry=="NL"
replace green=1 if prtvtbse==4 & cntry=="SE"
replace green=0 if prtvtbse!=4 & cntry=="SE"
replace green=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 

* others
gen others=.
replace others=1 if (prtvtbat==8 | prtvtbat==9 | prtvtbat==10) & cntry=="AT"
replace others=0 if (prtvtbat!=8 & prtvtbat!=9 & prtvtbat!=10) & cntry=="AT"
replace others=1 if (prtvtcbe==3 | prtvtbat==4 | prtvtbat==16) & cntry=="BE"
replace others=0 if (prtvtcbe!=3 & prtvtbat!=4 & prtvtbat!=16) & cntry=="BE"
replace others=1 if (prtvtdcz==4 | prtvtdcz==8) & cntry=="CZ"
replace others=0 if (prtvtdcz!=4 & prtvtdcz!=8) & cntry=="CZ"
replace others=1 if (prtvede2==7 | prtvede2==9) & cntry=="DE"
replace others=0 if (prtvede2!=7 & prtvede2!=9) & cntry=="DE"
replace others=1 if prtvtfee==10 & cntry=="EE"
replace others=0 if prtvtfee!=10 & cntry=="EE"
replace others=1 if (prtvtdfi==2 | prtvtdfi==3 | prtvtdfi==5 | prtvtdfi==7 | prtvtdfi==16)  & cntry=="FI"
replace others=0 if (prtvtdfi!=2 & prtvtdfi!=3 & prtvtdfi!=5 & prtvtdfi!=7 & prtvtdfi!=16)  & cntry=="FI"
replace others=1 if (prtvtbgb==4 | prtvtbgb==5 | prtvtbgb==8 | prtvtbgb==14 | prtvtbgb==16 | prtvtbgb==18) & cntry=="GB"
replace others=0 if (prtvtbgb!=4 & prtvtbgb!=5 & prtvtbgb!=8 & prtvtbgb!=14 & prtvtbgb!=16 & prtvtbgb!=18) & cntry=="GB"
replace others=1 if (prtvtfnl==9 | prtvtfnl==11 | prtvtfnl==16) & cntry=="NL"
replace others=0 if (prtvtfnl!=9 & prtvtfnl!=11 & prtvtfnl!=16) & cntry=="NL"
replace others=1 if (prtvtbse==1 | prtvtbse==8 | prtvtbse==9 | prtvtbse==11) & cntry=="SE"
replace others=0 if (prtvtbse==1 & prtvtbse!=8 & prtvtbse==9 & prtvtbse!=11) & cntry=="SE"
replace others=1 if (prtvtesi==1 | prtvtesi==10 | prtvtesi==12) & cntry=="SI"
replace others=0 if (prtvtesi!=1 & prtvtesi!=10 & prtvtesi!=12) & cntry=="SI"
replace others=. if (prtvtbat==.a | prtvtbat==.b | prtvtbat==.c) 


gen past_v=. 
replace past_v=1 if green==1
replace past_v=2 if socialist_rl==1
replace past_v=3 if liberal==1
replace past_v=4 if con_christdem==1
replace past_v=5 if rad_right==1
replace past_v=6 if others==1

label define lbpast_v 1"Green" 2"Socialist/Radical Left" 3"Liberal" 4"Conservatives/Christian Democrats" 5"Radical Right" 6"Others"
label values past_v lbpast_v

gen past_v2=. 
replace past_v2=1 if green==1 | socialist_rl==1
replace past_v2=2 if liberal==1
replace past_v2=3 if con_christdem==1
replace past_v2=4 if rad_right==1
replace past_v2=5 if others==1
label define lbpast_v2 1"Green/Socialist/Radical Left" 2"Liberal" 3"Conservatives/Christian Democrats" 4"Radical Right" 5"Others"
label values past_v2 lbpast_v2


**************************************
* create entropy weight (by country) *
**************************************
sort country
gen w_main=.
foreach x in "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" {
	ebalance tr c.age i.gender c.edu i.domicil i.chldhhe i.minority if country==`x', tar(2) 
	replace w_main=_webal if country==`x'
}
drop _webal
fre country


******************************
* create dummy for each NUTS *
******************************
encode region, generate(d_nuts) 
tab d_nuts tr, nol
forvalues i=1/131 {
    quietly levelsof tr if d_nuts==`i'
    if r(r)!=2 {
        di "NUTS == `i' does not contain two levels "
    }
}
*  0 out of 131 NUTS are imblanced

save "ess.dta", replace

* ----------------------------------------------------------------------------
**# Covariates balance tests (by country)  
* ----------------------------------------------------------------------------
use "ess.dta", replace
fre country
local countries 1 2 3 4 5 6 7 8 9 10 
local ctry_names Austria Belgium  "Czech Republic" Germany Estonia Finland UK Netherlands Sweden Slovenia 


matrix bt1 = J(10,10,.)
matrix rownames bt1 = `ctry_names'
matrix colnames bt1 = age p-value gender p-value edu p-value chldhhe p-value minority p-value

local r=1
foreach j in `countries' {
	local c=1
	foreach var in age gender edu chldhhe minority {
		reg tr `var' if country==`j'
		matrix bt1[`r',`c++'] = e(b)[1,1]
		matrix bt1[`r',`c++'] = 2*ttail(e(df_r),abs(_b[`var']/_se[`var']))
	}
	local r = `r'+1
}

matrix bt2 = J(10,10,.)
matrix rownames bt2 = `ctry_names'
matrix colnames bt2 = domicil1 p-value domicil2 p-value domicil3 p-value domicil4 p-value domicil5 p-value

local r=1
foreach j in `countries' {
	local c=1
	reg tr i.domicil if country==`j'
	matrix bt2[`r',`c++'] = e(b)[1,1]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[1.domicil]/_se[1.domicil]))
	matrix bt2[`r',`c++'] = e(b)[1,2]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[2.domicil]/_se[2.domicil]))
	matrix bt2[`r',`c++'] = e(b)[1,3]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[3.domicil]/_se[3.domicil]))
	matrix bt2[`r',`c++'] = e(b)[1,4]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[4.domicil]/_se[4.domicil]))
	matrix bt2[`r',`c++'] = e(b)[1,5]
	matrix bt2[`r',`c++'] = 2*ttail(e(df_r),abs(_b[5.domicil]/_se[5.domicil]))
	local r = `r'+1
}
matrix list bt2

putexcel set "balance tests.xls", sheet("Sheet 1", replace) replace 
putexcel A1 = matrix(bt1), names
putexcel set "balance tests.xls", sheet("Sheet 2", replace) modify
putexcel A1 = matrix(bt2), names
putexcel clear


* ----------------------------------------------------------------------------
**# Main effect
* ----------------------------------------------------------------------------
use "ess.dta", replace

* define control variables in the main model
global controls "c.age c.age#c.age i.gender c.edu i.chldhhe i.minority i.domicil" 

*******
* OLS *
*******
eststo main_c: reg swd i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": main_c
quietly estadd local cfe "Yes": main_c
quietly estadd local clust `e(N_clust)': main_c

************
* ebalance *
************
eststo e_main: reg swd i.tr i.d_nuts if date!=td(9nov2016) [pweight= w_main], vce(cluster region)
quietly estadd local ctrl "No": e_main
quietly estadd local cfe "Yes": e_main

********************************
* Recode SWD into binary (OLS) *
********************************
eststo b_main: reg swd_b i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": b_main
quietly estadd local cfe "Yes": b_main
quietly estadd local clust `e(N_clust)': b_main

**********************************
* Recode SWD into binary (Logit) *
**********************************
eststo logit_main: logit swd_b i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": logit_main
quietly estadd local cfe "Yes": logit_main
quietly estadd local clust `e(N_clust)': logit_main


**********
* ologit *
**********
eststo o_main: ologit swd i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes":o_main
quietly estadd local cfe "Yes": o_main
quietly estadd local clust `e(N_clust)': o_main


* regression table (in manuscript)
esttab main_c e_main b_main logit_main o_main using "ess_main.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") ///
	label wide note onecell noomitted nonumbers lines keep (1.tr) ///
	coeflabel(1.tr "Trump victory") varwidth(12) ///
	mtitles("(1) OLS (11-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight. In dichotomizing SWD, the dummy takes a value of 1 if the answer is above mid-point, and 0 if it is below mid-point.") ///
	title("Table 2. The spillover effect of Trump's electoral victory on citizens' SWD") 

	
* regression table (full with covariates)
esttab  main_c e_main b_main logit_main o_main using "ess_main_full.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") ///
	label wide note onecell noomitted nonumbers lines ///
	keep (1.tr age c.age#c.age 2.gender edu 2.domicil 3.domicil 4.domicil 5.domicil 2.chldhhe 2.minority ///
	/:cut1 /:cut2 /:cut3 /:cut4 /:cut5 /:cut6 /:cut7 /:cut8 /:cut9 /:cut10) ///
	coeflabel(1.tr "Trump victory") varwidth(20) ///
	mtitles("(1) OLS (11-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight. In dichotomizing SWD, the dummy takes a value of 1 if the answer is above mid-point, and 0 if it is below mid-point.") ///
	title("Table C.4. The spillover effect of Trump's electoral victory on citizens' SWD") 	

	
* ----------------------------------------------------------------------------
**# Duration of the effect
* ----------------------------------------------------------------------------	
eststo main_duration: reg swd ib5.biweek i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)	
coefplot main_duration, keep(*.biweek) base levels(90) vertical	///
	ytitle ("Change in SWD", size(med)) yline(0, lcolor(red) lpattern(solid)) ///
	xline (5.5, lcolor(blue)) xline (10, lcolor(brown) lpattern(dash)) ///
	text(0.4 5.3 "Trump's 2016 victory" 0.4 9.75 "Trump assumed office",  orient(vertical))  ///
	coeflabels(1.biweek = "Before Week -9" 2.biweek = "Week -8 to -7" 3.biweek = "Week -6 to -5" 4.biweek = "Week -4 to -3" ///
			5.biweek = "Week -2 to -1" 6.biweek = "Week 1 to 2" 7.biweek = "Week 3 to 4" 8.biweek = "Week 5 to 6" ///
			9.biweek = "Week 7 to 8" 10.biweek = "Week 9 to 10" 11.biweek = "Week 11 to 12" 12.biweek = "Week 13 onward", angle(45)) scheme(538bw)
graph export "ess_duration.png", replace	

	
* ----------------------------------------------------------------------------
**# Robustness checks
* ----------------------------------------------------------------------------
************************************************
* Adding Socioeconomics and Voting as controls * 
************************************************
eststo socecon: reg swd i.tr i.d_nuts $controls i.uemp3m i.hincfel if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": socecon
quietly estadd local cfe "Yes": socecon
quietly estadd local clust `e(N_clust)': socecon
quietly estadd local soceco "Yes": socecon
quietly estadd local vote "No": socecon

eststo vote: reg swd i.tr i.d_nuts $controls i.uemp3m i.hincfel i.vote if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": vote
quietly estadd local cfe "Yes": vote
quietly estadd local clust `e(N_clust)': vote
quietly estadd local soceco "Yes": vote
quietly estadd local vote "Yes": vote		
	
* regression table (full with covariates)	
esttab socecon vote using "socecon+vote_full.rtf", b(2) se(2) replace /// 
	scalars("ctrl Sociodemographic" "soceco Socioeconomics" "vote Voting" "cfe NUTS FE" "N Observations") 	///
	label wide note onecell nobaselevels noomitted nonumbers lines ///
	keep (1.tr age c.age#c.age 2.gender edu 2.domicil 3.domicil 4.domicil 5.domicil 2.chldhhe 2.minority 2.uemp3m 2.hincfel 3.hincfel 4.hincfel 2.vote 3.vote) ///
	coeflabel(1.tr "Trump victory") varwidth(28) ///
	mtitles("(1) Socioeconomics" "(2) Socioeconomics and Voting")  modelwidth(12) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients of the OLS; standard errors clustered at the NUTS level are shown in parentheses. Socio-demographic controls include age, square term of age, gender, level of education, children in household, ethnic minority status, type of community. Socioeconomic controls include unemployment for more than three months and feeling about household's income. Voting denotes turnout in the previous national election.") ///
	title("Table C.5. Adding socioeconomics and voting as controls") 	

********************
* Permutation test *
********************
permute tr _b[1.tr], strata(country) rep(5000): reg swd i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region) // stratify the permutation by country
	
**************
* Country FE *
**************
eststo main_c_ctry: reg swd i.tr i.country $controls if date!=td(9nov2016), vce(cluster region) 
quietly estadd local ctrl "Yes": main_c_ctry
quietly estadd local cfe "Yes": main_c_ctry
quietly estadd local clust `e(N_clust)': main_c_ctry

* ebalance 
eststo e_main_ctry: reg swd i.tr i.country if date!=td(9nov2016) [pweight= w_main], vce(cluster region) 
quietly estadd local ctrl "No": e_main_ctry
quietly estadd local cfe "Yes": e_main_ctry

* Recode SWD into binary (OLS) *
eststo b_main_ctry: reg swd_b i.tr i.country $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": b_main_ctry
quietly estadd local cfe "Yes": b_main_ctry
quietly estadd local clust `e(N_clust)': b_main_ctry

* Recode SWD into binary (Logit) *
eststo logit_main_ctry: logit swd_b i.tr i.country $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": logit_main_ctry
quietly estadd local cfe "Yes": logit_main_ctry
quietly estadd local clust `e(N_clust)': logit_main_ctry

* ologit
eststo o_main_ctry: ologit swd i.tr i.country $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes":o_main_ctry
quietly estadd local cfe "Yes": o_main_ctry
quietly estadd local clust `e(N_clust)': o_main_ctry

* regression table (full with covariates)	
esttab  main_c_ctry e_main_ctry b_main_ctry logit_main_ctry o_main_ctry using "ess_ctry_fe_full.rtf", b(2) se(2) replace /// 
	scalars("cfe Country FE" "ctrl Controls" "N Observations") 	///
	label wide note onecell noomitted nonumbers lines ///
	keep (1.tr age c.age#c.age 2.gender edu 2.domicil 3.domicil 4.domicil 5.domicil 2.chldhhe 2.minority ///
	/:cut1 /:cut2 /:cut3 /:cut4 /:cut5 /:cut6 /:cut7 /:cut8 /:cut9 /:cut10) ///
	coeflabel(1.tr "Trump victory") varwidth(20) ///
	mtitles("(1) OLS (11-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls  (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight. In dichotomizing SWD, the dummy takes a value of 1 if the answer is above mid-point, and 0 if it is below mid-point.") ///
	title("Table C.7. Using country fixed effects") 	
	

* ----------------------------------------------------------------------------
**# Heterogenous effect
* ----------------------------------------------------------------------------
*** Past vote choice
reg swd i.tr##i.past_v i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v: margins past_v, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v
quietly estadd local cfe "Yes": past_v

marginsplot, title(" ", size(medium)) ///
	ytitle("Marginal effect of Trump's victory on SWD", size(medium)) ///
	xtitle(" " "Party family of previous vote", size(medium))  /// 
	xlabel(1"Green" 2`" "Socialist /" "Radical Left""' 3"Liberal" 4`" "Conservatives /" "Christian Democrats""' 5"Radical Right" 6"Others") ///
	yline(0, lwidth(2pt) lcolor(red)) ///
	plotopts(lcolor(black) lpattern("blank")) ciopts(color(black)) graphregion(margin(r+3)) /// (lpattern("blank")) graphregion(margin(r+3))  scheme(538bw) 
	saving(past_v_ess, replace)
graph export "past_v_ess.png", replace

reg swd i.tr##i.past_v i.d_nuts if date!=td(9nov2016) [pweight= w_main], vce(cluster region)
eststo past_v_e: margins past_v, dydx(tr) post level(95)
quietly estadd local ctrl "No": past_v_e
quietly estadd local cfe "Yes": past_v_e

reg swd_b i.tr##i.past_v i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v_b: margins past_v, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v_b
quietly estadd local cfe "Yes": past_v_b

logit swd_b i.tr##i.past_v i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v_logit: margins past_v, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v_logit
quietly estadd local cfe "Yes": past_v_logit


esttab past_v past_v_e past_v_b past_v_logit using "past_v_dydx.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel(1.past_v "Green" 2.past_v "Socialist/Radical Left" 3.past_v "Liberal" 4.past_v "Conservatives/Christian Democrats" 5.past_v "Radical Right" 6.past_v "Others") varwidth(12) ///	
	mtitles("OLS (11-point scale)" "Entropy Balancing" "OLS (dichotomize SWD)" "Logit (dichotomize SWD)")  modelwidth(8) ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. Standard errors are shown in parentheses and are clustered at the NUTS level. Models 1, 3 and 4 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.2. Marginal effect of Trump's electoral victory across voters of various party families") 	

	
ologit swd i.tr##i.past_v i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v_ologit: margins, dydx(tr) post level(95) at(past_v=(1(1)6))
quietly estadd local ctrl "Yes": past_v_ologit
quietly estadd local cfe "Yes": past_v_ologit	
	

* The table in the appendix is re-arranged to aid reading	
esttab past_v_ologit using "past_v_dydx_ologit.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel (1._predict#1._at "Green (Point 1)" 1._predict#2._at "Socialist/Radical Left (Point 1)" 1._predict#3._at "Liberal" 1._predict#4._at "Conservatives/Christian Democrats (Point 1)" 1._predict#5._at "Radical Right (Point 1)" 1._predict#6._at "Others (Point 1)" /// 
	2._predict#1._at "Green (Point 2)" 2._predict#2._at "Socialist/Radical Left (Point 2)" 2._predict#3._at "Liberal (Point 2)" 2._predict#4._at "Conservatives/Christian Democrats (Point 2)" 2._predict#5._at "Radical Right (Point 2)" 2._predict#6._at "Others (Point 2)" ///
	3._predict#1._at "Green (Point 3)" 3._predict#2._at "Socialist/Radical Left (Point 3)" 3._predict#3._at "Liberal (Point 3)" 3._predict#4._at "Conservatives/Christian Democrats (Point 3)" 3._predict#5._at "Radical Right (Point 3)" 3._predict#6._at "Others (Point 3)" ///
	4._predict#1._at "Green (Point 4)" 4._predict#2._at "Socialist/Radical Left (Point 4)" 4._predict#3._at "Liberal (Point 4)" 4._predict#4._at "Conservatives/Christian Democrats (Point 4)" 4._predict#5._at "Radical Right (Point 4)" 4._predict#6._at "Others (Point 4)" ///
	5._predict#1._at "Green (Point 5)" 5._predict#2._at "Socialist/Radical Left (Point 5)"5._predict#3._at "Liberal (Point 5)" 5._predict#4._at "Conservatives/Christian Democrats (Point 5)" 5._predict#5._at "Radical Right (Point 5)" 5._predict#6._at "Others (Point 5)" ///
	6._predict#1._at "Green (Point 6)" 6._predict#2._at "Socialist/Radical Left (Point 6)" 6._predict#3._at "Liberal (Point 6)" 6._predict#4._at "Conservatives/Christian Democrats (Point 6)" 6._predict#5._at "Radical Right (Point 6)" 6._predict#6._at "Others (Point 6)" ///
	7._predict#1._at "Green (Point 7)" 7._predict#2._at "Socialist/Radical Left (Point 7)" 7._predict#3._at "Liberal (Point 7)" 7._predict#4._at "Conservatives/Christian Democrats (Point 7)" 7._predict#5._at "Radical Right (Point 7)" 7._predict#6._at "Others (Point 7)" ///
	8._predict#1._at "Green (Point 8)" 8._predict#2._at "Socialist/Radical Left (Point 8)" 8._predict#3._at "Liberal (Point 8)" 8._predict#4._at "Conservatives/Christian Democrats (Point 8)" 8._predict#5._at "Radical Right (Point 8)" 8._predict#6._at "Others (Point 8)" ///
	9._predict#1._at "Green (Point 9)" 9._predict#2._at "Socialist/Radical Left (Point 9)" 9._predict#3._at "Liberal (Point 9)" 9._predict#4._at "Conservatives/Christian Democrats (Point 9)" 9._predict#5._at "Radical Right (Point 9)" 9._predict#6._at "Others (Point 9)" ///
	10._predict#1._at "Green (Point 10)" 10._predict#2._at "Socialist/Radical Left (Point 10)" 10._predict#3._at "Liberal (Point 10)" 10._predict#4._at "Conservatives/Christian Democrats (Point 10)" 10._predict#5._at "Radical Right (Point 10)" 10._predict#6._at "Others (Point 10)" ///
	11._predict#1._at "Green (Point 11)" 11._predict#2._at "Socialist/Radical Left (Point 11)" 11._predict#3._at "Liberal (Point 11)" 11._predict#4._at "Conservatives/Christian Democrats (Point 11)" 11._predict#5._at "Radical Right (Point 11)" 11._predict#6._at "Others (Point 11)") varwidth(25) ///	
	mtitles(" ")  modelwidth(8) ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. DV: 11-point scale of SWD. Standard errors are shown in parentheses and are clustered at the NUTS level. The model includes socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.3. Marginal effect of Trump's electoral victory across voters of various party families (ordered logit)") 	
	

*** Combine Green with Socialist/Radical Left
eststo past_v2: reg swd i.tr##i.past_v2 i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v2: margins past_v2, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v2
quietly estadd local cfe "Yes": past_v2

eststo past_v2_e: reg swd i.tr##i.past_v2 i.d_nuts if date!=td(9nov2016) [pweight= w_main], vce(cluster region)
eststo past_v2_e: margins past_v2, dydx(tr) post level(95)
quietly estadd local ctrl "No": past_v2_e
quietly estadd local cfe "Yes": past_v2_e

eststo past_v2_b: reg swd_b i.tr##i.past_v2 i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v2_b: margins past_v2, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v2_b
quietly estadd local cfe "Yes": past_v2_b

eststo past_v2_logit: logit swd_b i.tr##i.past_v2 i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v2_logit: margins past_v2, dydx(tr) post level(95)
quietly estadd local ctrl "Yes": past_v2_logit
quietly estadd local cfe "Yes": past_v2_logit

esttab past_v2 past_v2_e past_v2_b past_v2_logit using "past_v2_dydx.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel(1.past_v2 "Green/Socialist/Radical Left" 2.past_v2 "Liberal" 3.past_v2 "Conservatives/Christian Democrats" 4.past_v2 "Radical Right" 5.past_v2 "Others") varwidth(12) ///	
	mtitles("OLS (11-point scale)" "Entropy Balancing" "OLS (dichotomize SWD)" "Logit (dichotomize SWD)")  modelwidth(8) ///
	star(* 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. Standard errors are shown in parentheses and are clustered at the NUTS level. Models 1, 3 and 4 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.4. Combining Green with Socialist/Radical Left") 


ologit swd i.tr##i.past_v2 i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
eststo past_v2_ologit: margins, dydx(tr) post level(95) at(past_v=(1(1)5))
quietly estadd local ctrl "Yes": past_v2_ologit
quietly estadd local cfe "Yes": past_v2_ologit	
	
	
* The table in the appendix is re-arranged to aid reading	
esttab past_v2_ologit using "past_v2_dydx_ologit.rtf", b(2) se(2) replace ///
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations")  ///
	coeflabel (1._predict#1._at "Green/Socialist/Radical Left (Point 1)" 1._predict#2._at "Liberal (Point 1)" 1._predict#3._at "Conservatives/Christian Democrats" 1._predict#4._at "Radical Right (Point 1)" 1._predict#5._at "Others (Point 1)" /// 
	2._predict#1._at "Green/Socialist/Radical Left (Point 2)" 2._predict#2._at "Liberal (Point 2)" 2._predict#3._at "Conservatives/Christian Democrats (Point 2)" 2._predict#4._at "Radical Right (Point 2)" 2._predict#5._at "Others (Point 2)" ///
	3._predict#1._at "Green/Socialist/Radical Left (Point 3)" 3._predict#2._at "Liberal (Point 3)" 3._predict#3._at "Conservatives/Christian Democrats (Point 3)" 3._predict#4._at "Radical Right (Point 3)" 3._predict#5._at "Others (Point 3)" ///
	4._predict#1._at "Green/Socialist/Radical Left (Point 4)" 4._predict#2._at "Liberal (Point 4)" 4._predict#3._at "Conservatives/Christian Democrats (Point 4)" 4._predict#4._at "Radical Right (Point 4)" 4._predict#5._at "Others (Point 4)" ///
	5._predict#1._at "Green/Socialist/Radical Left (Point 5)" 5._predict#2._at "Liberal (Point 5)"5._predict#3._at "Conservatives/Christian Democrats (Point 5)" 5._predict#4._at "Radical Right (Point 5)" 5._predict#5._at "Others (Point 5)" ///
	6._predict#1._at "Green/Socialist/Radical Left (Point 6)" 6._predict#2._at "Liberal (Point 6)" 6._predict#3._at "Conservatives/Christian Democrats (Point 6)" 6._predict#4._at "Radical Right (Point 6)" 6._predict#5._at "Others (Point 6)" ///
	7._predict#1._at "Green/Socialist/Radical Left (Point 7)" 7._predict#2._at "Liberal (Point 7)" 7._predict#3._at "Conservatives/Christian Democrats (Point 7)" 7._predict#4._at "Radical Right (Point 7)" 7._predict#5._at "Others (Point 7)" ///
	8._predict#1._at "Green/Socialist/Radical Left (Point 8)" 8._predict#2._at "Liberal (Point 8)" 8._predict#3._at "Conservatives/Christian Democrats (Point 8)" 8._predict#4._at "Radical Right (Point 8)" 8._predict#5._at "Others (Point 8)" ///
	9._predict#1._at "Green/Socialist/Radical Left (Point 9)" 9._predict#2._at "Liberal (Point 9)" 9._predict#3._at "Conservatives/Christian Democrats (Point 9)" 9._predict#4._at "Radical Right (Point 9)" 9._predict#5._at "Others (Point 9)" ///
	10._predict#1._at "Green/Socialist/Radical Left (Point 10)" 10._predict#2._at "Liberal (Point 10)" 10._predict#3._at "Conservatives/Christian Democrats (Point 10)" 10._predict#4._at "Radical Right (Point 10)" 10._predict#5._at "Others (Point 10)" ///
	11._predict#1._at "Green/Socialist/Radical Left (Point 11)" 11._predict#2._at "Liberal (Point 11)" 11._predict#3._at "Conservatives/Christian Democrats (Point 11)" 11._predict#4._at "Radical Right (Point 11)" 11._predict#5._at "Others (Point 11)") varwidth(25) ///	
	mtitles(" ")  modelwidth(8) ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are marginal effects of the treatment. DV: 11-point scale of SWD. Standard errors are shown in parentheses and are clustered at the NUTS level. The model includes socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.5. Marginal effect of Trump's electoral victory across voters of various party families (ordered logit)") 	
	
* balance tests for past vote choice
eststo green: reg green i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": green
quietly estadd local cfe "Yes": green
eststo socialist_rl: reg socialist_rl i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": socialist_rl
quietly estadd local cfe "Yes": socialist_rl
eststo liberal: reg liberal  i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": liberal
quietly estadd local cfe "Yes": liberal
eststo con_christdem: reg con_christdem i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": con_christdem
quietly estadd local cfe "Yes": con_christdem
eststo rad_right: reg rad_right i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": rad_right
quietly estadd local cfe "Yes": rad_right
eststo others: reg others i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": others
quietly estadd local cfe "Yes": others

esttab green socialist_rl liberal con_christdem rad_right others using "missing_ess.rtf", b(2) se(2) replace ///
	scalars("cfe Country FE" "ctrl Controls" "N Observations") 	keep (1.tr) ///
	coeflabel(1.tr "Trump victory") varwidth(12) ///
	mtitles("Green" "Socialist/Radical Left" "Liberal" "Conservatives/Christian Democrats" "Radical Right" "Others")  modelwidth(6) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	note("Note: The DV is a dummy coded as 1 if the respondent answers that he/she voted for the corresponding party family and 0 otherwise. Entries are coefficients of OLS by regressing the DV on the treatment indicator. Standard errors clustered at the NUTS level are shown in parentheses. All models include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.6. Balance tests of party family of the past vote choice") 	
	
	
*** Moderating role of left/right self-placement
* balance test of left/right self-placement
eststo lr_b: reg lr i.tr i.d_nuts $controls if date!=td(9nov2016), vce(cluster region)
quietly estadd local ctrl "Yes": lr_b
quietly estadd local cfe "Yes": lr_b
esttab lr_b using "lr_b_ess.rtf", b(2) se(2) replace ///
	scalars("cfe Country FE" "ctrl Controls" "N Observations") 	keep (1.tr) ///
	coeflabel(1.tr "Trump victory") varwidth(12) ///
	mtitles("Ideological Self-placement")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	note("Note: The DV is ideological self-placement on an 11-point scale. Entries are coefficients of OLS by regressing the DV on the treatment indicator. Standard errors clustered at the NUTS level are shown in parentheses. The model includes socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community).") ///
	title("Table D.7. Balance tests of ideological self-placement") 

* Figure D.1 
interflex swd tr lr $control if date!=td(9nov2016), fe(d_nuts) cl(region) vce(cluster)  ///
	ylabel(SWD) xlabel(Ideological Self-placement) dlabel(Trump's victory) 
	graph export "lr_ess_interflex.png", replace	


* ----------------------------------------------------------------------------
**# Placebo using 2012 
* ----------------------------------------------------------------------------
use "ESS6e02_5.dta", replace 
set scheme white_ptol

* Data preparation
**********************
* treatment variable *
**********************
fre inwdds 
fre inwmms 
fre inwyys
gen date=mdy(inwmms,inwdds,inwyys)
format date %tdCCYY-NN-DD
list date in 1/20
gen day = date - td(7nov2012) 
fre day

gen tr=.
replace tr=0 if date<=td(6nov2012) 
replace tr=1 if date>td(7nov2012) 
replace tr=. if date==.
fre tr

***********
* country *
***********
fre cntry
tab cntry tr 
keep if  cntry=="BE" |  cntry=="CY"  |  cntry=="DE" | cntry=="EE" | cntry=="FI" | cntry=="GB" | cntry=="IE" | cntry=="NL" | cntry=="PL" | cntry=="SE"| cntry=="SI"  
tab cntry tr 
encode cntry, generate(country)
tab date tr 
fre country

***********
* outcome *
***********
* satisfaction with democracy (11-pt scale)
fre stfdem 
rename stfdem swd

* binary
gen swd_b=. 
replace swd_b=1 if swd>5
replace swd_b=0 if swd<5
replace swd_b=. if swd==.a | swd==.b
tab swd swd_b, m

**************
* covariates * 
**************
rename agea age
label var age "Age"
rename gndr gender 

rename eisced edu
fre edu
replace edu=. if edu==55 // replace "other" to missing 
fre edu
label var edu "Level of education"

fre domicil //type of community

fre chldhhe //have children living in household
label define lbchldhhe  1"Children living in household: Yes" 2"Children living in household: No" 
label values chldhhe lbchldhhe 

rename blgetmg minority // Belong to minority ethnic group in country
label define lbminority 1"Minority ethnic group: Yes" 2"Minority ethnic group: No " 
label values minority lbminority

* define control variables in the main model
global controls "c.age c.age#c.age i.gender c.edu i.chldhhe i.minority i.domicil" 

*************************
* create entropy weight *
*************************
sort country
gen w_main=.
foreach x in "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11"  {
	ebalance tr c.age i.gender c.edu i.domicil i.chldhhe i.minority if country==`x', tar(2) 
	replace w_main=_webal if country==`x'
}
drop _webal

******************************
* create dummy for each NUTS *
******************************
encode region, generate(d_nuts) 
tab d_nuts tr, nol
forvalues i=1/131 {
    quietly levelsof tr if d_nuts==`i'
    if r(r)!=2 {
        di "NUTS == `i' does not contain two levels "
    }
}
*  3 out of 131 NUTS are imblanced


*********************
* main effect (OLS) *
*********************
eststo pcbo_main_c: reg swd i.tr i.d_nuts $controls if date!=td(7nov2012), vce(cluster region)
quietly estadd local ctrl "Yes": pcbo_main_c
quietly estadd local cfe "Yes": pcbo_main_c
quietly estadd local clust `e(N_clust)': pcbo_main_c

************
* ebalance *
************
eststo pcbo_e_main: reg swd i.tr i.d_nuts if date!=td(7nov2012) [pweight= w_main], vce(cluster region)
quietly estadd local ctrl "No": pcbo_e_main
quietly estadd local cfe "Yes": pcbo_e_main

********************************
* Recode SWD into binary (OLS) *
********************************
eststo pcbo_b_main: reg swd_b i.tr i.d_nuts $controls if date!=td(7nov2012), vce(cluster region)
quietly estadd local ctrl "Yes": pcbo_b_main
quietly estadd local cfe "Yes": pcbo_b_main
quietly estadd local clust `e(N_clust)': pcbo_b_main

********************************
* Recode SWD into binary (Logit) *
********************************
eststo pcbo_logit_main: logit swd_b i.tr i.d_nuts $controls if date!=td(7nov2012), vce(cluster region)
quietly estadd local ctrl "Yes": pcbo_logit_main
quietly estadd local cfe "Yes": pcbo_logit_main
quietly estadd local clust `e(N_clust)': pcbo_logit_main


**********
* ologit *
**********
eststo pcbo_o_main: ologit swd i.tr i.d_nuts $controls if date!=td(7nov2012), vce(cluster region)
quietly estadd local ctrl "Yes":pcbo_o_main
quietly estadd local cfe "Yes": pcbo_o_main
quietly estadd local clust `e(N_clust)': pcbo_o_main


* regression table (full with covariates)
esttab  pcbo_main_c pcbo_e_main pcbo_b_main pcbo_logit_main pcbo_o_main using "ess_pcbo.rtf", b(2) se(2) replace /// 
	scalars("cfe NUTS FE" "ctrl Controls" "N Observations") 	///
	label wide note onecell noomitted nonumbers lines ///
	keep (1.tr age c.age#c.age 2.gender edu 2.domicil 3.domicil 4.domicil 5.domicil 2.chldhhe 2.minority ///
	/:cut1 /:cut2 /:cut3 /:cut4 /:cut5 /:cut6 /:cut7 /:cut8 /:cut9 /:cut10) ///
	coeflabel(1.tr "Obama victory") varwidth(20) ///
	mtitles("(1) OLS (11-point scale)" "(2) Entropy Balancing" "(3) OLS (dichotomize SWD)" "(4) Logit (dichotomize SWD)" "(5) Ordered Logit")  modelwidth(8) ///
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)  ///
	note("Note: Entries are coefficients; standard errors clustered at the NUTS level are shown in parentheses. Models 1, 3-5 include socio-demographic controls (age, square term of age, gender, level of education, children in household, ethnic minority status, type of community). Model 2 is entropy-balanced using the aforementioned socio-demographic variables; the highest order of moment constraints is set at the variance for each covariate when creating the entropy weight. In dichotomizing SWD, the dummy takes a value of 1 if the answer is above mid-point, and 0 if it is below mid-point.") ///
	title("Table C.8. Placebo test using Obama's victory in the 2012 election") 	